/*
 * 版权所有 (C) 2010-2012 WWW.GONUP.ORG。保留所有权利。
 * 版本：
 * 修改记录：
 *        1、2012-8-2，zhichenggang创建。
 */
package net.redxyz.repost.resource.action.plan;

import net.redxyz.repost.resource.SessionUser;
import net.redxyz.top.repost.dbo.AssRepostPlanDetail;

import org.zhiqim.httpd.HttpRequest;
import org.zhiqim.httpd.context.annotation.AnIntercept;
import org.zhiqim.httpd.context.core.Action;
import org.zhiqim.kernel.util.Validates;
import org.zhiqim.orm.ORM;
import org.zhiqim.orm.dbo.Selector;
import org.zhiqim.orm.dbo.Updater;

/**
 * 均衡上下架计划调整时间
 *
 * @version v7.0.1 @author zouzhigang 2021-7-29 新建与整理
 */
public class RepostPlanDetailTimeAction implements Action
{

    public void execute(HttpRequest request) throws Exception
    {
        SessionUser sessionUser = request.getSessionUser(SessionUser.class);
        String planId = request.getParameter("planId");
        long numIid = request.getParameterLong("numIid");
        if (Validates.isEmpty(planId) || numIid == -1)
        {
            request.returnCloseDialog("请选择一个宝贝");
            return;
        }

        Selector selector = new Selector();
        selector.addMaybe("planId", planId);
        selector.addMaybe("numIid", numIid);
        selector.addReplace("d", "001");

        AssRepostPlanDetail planDetail = ORM.table().item(AssRepostPlanDetail.class, selector);
        if (planDetail == null || !planDetail.getNick().equals(sessionUser.getNick()))
        {
            request.returnCloseDialog("请选择一个有效的宝贝");
            return;
        }

        request.setAttribute("planDetail", planDetail);
    }

    @AnIntercept("chkAjax")
    public String doUpdateDetailTime(HttpRequest request, String planId, long numIid, int newWeek, String newTime) throws Exception
    {
        newTime = newTime + ":00";
        if (Validates.isEmpty(planId) || numIid == -1 || newWeek < 1 || newWeek > 7 || !Validates.isTime(newTime))
        {
            return "请选择一个正确的宝贝";
        }

        SessionUser sessionUser = request.getSessionUser(SessionUser.class);

        Selector selector = new Selector();
        selector.addMaybe("planId", planId);
        selector.addMaybe("numIid", numIid);
        selector.addReplace("d", "001");
        AssRepostPlanDetail planDetail = ORM.table().item(AssRepostPlanDetail.class, selector);
        if (planDetail == null || !planDetail.getNick().equals(sessionUser.getNick()))
        {
            return "请选择一个有效的宝贝";
        }

        if (newWeek == planDetail.getPlanWeek() && newTime.equals(planDetail.getPlanTime()))
        {
            return "时间相同，无需调整";
        }

        Updater updater = new Updater();
        updater.addMaybe("planId", planId);
        updater.addMaybe("numIid", numIid);
        updater.addField("planWeek", newWeek);
        updater.addField("planTime", newTime);
        updater.addField("operateStatus", 0);
        updater.addField("operateTime", null);
        updater.addReplace("d", "001");

        ORM.table().update(AssRepostPlanDetail.class, updater);
        return "修改成功";
    }
}
